package planspiel;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;

/**
 * 
 * @author Administrator
 */
public abstract class AbstractTimer {
    /**
     * 
     */
    public final static int INTERVALL = 1; // in Sekunden
        /**
         * 
         */
        public final static int START = 300; // in Sekunden
	
	private int restzeit = START; // in Sekunden
	private Timer timer;
	
        /**
         * 
         */
        protected List<JLabel> changedListener = new ArrayList<JLabel>();
        /**
         * 
         */
        protected List<JLabel> finishedListener = new ArrayList<JLabel>();
	
        /**
         * 
         */
        public void decrement() {
		restzeit -= INTERVALL;
		fireChangedEvent();
	}

	/**
	 * @param label sollte besser ein eigenes Interface mit Callback-Methode sein
	 */
	public void addChangedListener(JLabel label) {
		changedListener.add(label);
	}

        /**
         * 
         * @param label
         */
        public void addFinishedListener(JLabel label) {
		finishedListener.add(label);
	}
	
        /**
         * 
         */
        abstract public void fireChangedEvent();

        /**
         * 
         */
        abstract public void fireFinishedEvent();

        /**
         * 
         */
        public void startTimer() {
		TimerTask task = new TimerTask() {

			@Override
            public void run() {
				AbstractTimer.this.decrement();
            }
        };
		
	    timer = new Timer();
	    // sofort starten, dann jede Sekunde wiederholen, alle Angaben in Millisekunden
	    timer.schedule(task, 0, AbstractTimer.INTERVALL * 1000);
	}

	/**
	 * @return the restzeit
	 */
	protected int getRestzeit() {
		return restzeit;
	}
	/**
	 * @return the timer
	 */
	protected Timer getTimer() {
		return timer;
	}
	
}
